//
//  DeviceListPageController.m
//  O2Cat
//
//  Created by xiabin on 15/12/17.
//  Copyright © 2015年 QiDao.Purifier. All rights reserved.
//

#import "DeviceInfoPageController.h"
#import "GizWifiManager.h"
#import <XPGWifiSDK/XPGWifiDevice.h>
#import "AddDeviceS1EnterController.h"

@interface DeviceInfoPageController ()
{
    NSMutableArray* devicesArray;
    NSDictionary* deviceData;
    int getDeviceJobId;
}

@property (nonatomic, strong) UITableView* itemTableView;
@property (nonatomic, strong) UIImageView* emptyImage;
@end

@implementation DeviceInfoPageController


#pragma mark life cycle

- (void) dealloc {
    _itemTableView.delegate = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    deviceData = [[NSDictionary alloc] initWithObjectsAndKeys:
            @"2015-10-14", @"激活时间",
            @"12天25小时", @"使用时长",
            @"QD102142", @"设备型号",nil];
    
    [self.view addSubview:self.itemTableView];
    [self.view addSubview:self.emptyImage];
    [self layoutSubviews];
    self.view.backgroundColor = QGPageBG;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void) viewWillAppear:(BOOL)animated {
    [self initNavigationStyle];
   
}

-(void) viewWillDisappear:(BOOL)animated {
}

#pragma mark ui event response
-(void) back {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark ui ctrl delegate

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(deviceData.allKeys.count == 0) {
        self.emptyImage.hidden = NO;
    } else {
        self.emptyImage.hidden = YES;
    }
    return deviceData.allKeys.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"DeviceItemCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
    }
    
    if (indexPath.row < deviceData.allKeys.count) {
        NSString* key = deviceData.allKeys[indexPath.row];
        NSString* value = deviceData[key];
        
        cell.textLabel.text = key;
        cell.detailTextLabel.text = value;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}



#pragma mark private methods

- (void) initNavigationStyle {
    UIView *statusBarView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_SIZE_WIDTH, 20)];
    statusBarView.backgroundColor=QGNavBG;
    [self.view addSubview:statusBarView];
    
    UIButton* cancelBtn = [Utils NavButtonStyleNormalBLeft:YES NormalImg:@"back" AndHightLight:@"back"];
    [cancelBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:cancelBtn];
    self.navigationItem.title = @"设备信息";
    
    /*UIButton* addBtn = [Utils NavButtonStyleNormalBLeft:NO NormalImg:@"addicon" AndHightLight:@"addicon"];
    [addBtn addTarget:self action:@selector(doAddDevice) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:addBtn];*/
}

-(void) layoutSubviews {
    __weak __typeof(&*self)weakSelf = self;
    [self.itemTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.view.mas_top);
        make.left.mas_equalTo(weakSelf.view.mas_left);
        make.height.mas_equalTo(weakSelf.view.mas_height);
        make.width.mas_equalTo(weakSelf.view.mas_width);
    }];
}

#pragma mark custom delegate
-(void) UMAsyncJobDone:(UMJobStatus)emJobStatus JobCookie:(int)JobID And:(id)JobRet {
/*    if(emJobStatus != SUCCESS) return;
    NSArray* devLists = (NSArray*) JobRet;
    [devicesArray removeAllObjects];
    for (id dev in devLists) {
        [devicesArray addObject:dev];
    }
    [self.itemTableView reloadData];*/
}

#pragma mark getter and setter

-(UITableView*) itemTableView {
    if (_itemTableView == nil) {
        _itemTableView = [[UITableView alloc] init];
        _itemTableView.backgroundColor = [UIColor whiteColor];
        _itemTableView.delegate = self;
        _itemTableView.dataSource = self;
        _itemTableView.allowsSelection = YES;
        _itemTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        _itemTableView.tableFooterView = [UIView new];
        _itemTableView.separatorColor = QGPageBG;
        _itemTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    }
    return _itemTableView;
}

-(UIImageView*) emptyImage {
    if(_emptyImage == nil) {
        _emptyImage = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_SIZE_WIDTH/3, SCREEN_SIZE_HEIGHT/3, SCREEN_SIZE_WIDTH/3, SCREEN_SIZE_HEIGHT/3)];
        _emptyImage.image = [UIImage imageNamed:@"emptylike"];
        _emptyImage.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _emptyImage;
}

@end
